package com.zhongfl.crm.service.impl.boss;

import com.zhongfl.crm.enums.boss.AmibaCode;
import com.zhongfl.crm.enums.boss.AmibaStaffRole;
import com.zhongfl.crm.lixiao.mapper.AmibaMapper;
import com.zhongfl.crm.lixiao.mapper.AmibaStaffMapper;
import com.zhongfl.crm.lixiao.mapper.UserWechatMapper;
import com.zhongfl.crm.service.boss.AmibaStaffService;
import com.zhongfl.crm.model.boss.Amiba;
import com.zhongfl.crm.model.boss.AmibaStaff;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tech.heptagram.channel.api.common.AssertUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author wang.yuchuan
 * @date 2018/5/3
 */
@Service
public class AmibaStaffServiceImpl implements AmibaStaffService {

    @Resource
    private AmibaStaffMapper amibaStaffMapper;

    @Resource
    private AmibaMapper amibaMapper;

    @Resource
    private UserWechatMapper userWechatMapper;

    /**
     * @param amibaStaff 阿米巴-员工关系
     * @see AmibaStaffService#saveAmibaStaff(AmibaStaff)
     */
    @Override
    public void saveAmibaStaff(AmibaStaff amibaStaff) {
        AssertUtils.notNull(amibaStaff, "要保存的阿米巴员工为Null");
        AssertUtils.hasText(amibaStaff.getCreatedBy(), "创建人不允许为空");
        AssertUtils.greaterThanZero(amibaStaff.getAmibaId(), "阿米巴Id不能为空");
        AssertUtils.hasText(amibaStaff.getStaffCode(), "员工编号不允许为空");
        AssertUtils.hasText(amibaStaff.getStaffName(), "员工姓名不允许为空");
        AssertUtils.notNull(amibaStaff.getRole(), "员工角色不允许为空");

        if (StringUtils.isEmpty(amibaStaff.getModifiedBy())) {
            amibaStaff.setModifiedBy(amibaStaff.getCreatedBy());
        }

        amibaStaffMapper.insertSelective(amibaStaff);
    }

    /**
     * @param amibaStaffs 员工列表
     * @see AmibaStaffService#saveBatch(List)
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveBatch(List<AmibaStaff> amibaStaffs) {
        for (AmibaStaff amibaStaff : amibaStaffs) {
            AmibaStaff staff = findByStaffCodeAndAmibaId(amibaStaff.getStaffCode(), amibaStaff.getAmibaId());
            // 同一个人在同一个阿米巴不需要两条
            if (Objects.nonNull(staff)) {
                continue;
            }
            saveAmibaStaff(amibaStaff);
        }
    }

    ///**
    // * 把已经删除的员工恢复成未删除状态
    // */
    //private void reset(AmibaStaff amibaStaff){
    //    AssertUtils.notNull(amibaStaff,"员工信息为空！");
    //    AssertUtils.notNull(amibaStaff.getAmibaId(),"员工阿米巴id为空！");
    //    AssertUtils.notNull(amibaStaff.getStaffCode(),"员工code信息为空！");
    //    amibaStaffMapper.reset(amibaStaff.getAmibaId(),amibaStaff.getStaffCode());
    //}

    @Override
    public void updateAmibaStaff(AmibaStaff amibaStaff) {
        AssertUtils.notNull(amibaStaff, "要更新的阿米巴员工为Null");
        AssertUtils.greaterThanZero(amibaStaff.getId(), "要更新的阿米巴ID必须指定");

        amibaStaffMapper.updateByPrimaryKeySelective(amibaStaff);
    }

    @Override
    public void deleteAmibaStaff(Integer id) {
        if(Objects.isNull(id) || id <= 0){
            return;
        }

        amibaStaffMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByAmibaId(Integer amibaId) {
        amibaStaffMapper.deleteByAmibaId(amibaId);
    }

    @Override
    public void deleteBatchByAmibaId(List<Integer> amibaIds) {
        if(Objects.isNull(amibaIds) || amibaIds.size() == 0){
            return;
        }
        amibaStaffMapper.deleteBatchByAmibaId(amibaIds);
    }

    /**
     * @see AmibaStaffService#findByAmibaId(Integer)
     */
    @Override
    public List<AmibaStaff> findByAmibaId(Integer amibaId) {
        if (Objects.isNull(amibaId) || amibaId <= 0) {
            return new ArrayList<>();
        }
        List<AmibaStaff> amibaStaffs = amibaStaffMapper.selectByAmibaId(amibaId);
        for (AmibaStaff amibaStaff : amibaStaffs) {
            amibaStaff.setRoleName(AmibaStaffRole.valueOf(amibaStaff.getRole()).getDesc());
        }

        return amibaStaffs;
    }

    @Override
    public List<AmibaStaff> findByAmibaIds(List<Integer> amibaIds) {
        return amibaStaffMapper.selectByAmibaIds(amibaIds);
    }

    @Override
    public List<AmibaStaff> findByAmibaIdsAll(List<Integer> amibaIds) {
        if(CollectionUtils.isEmpty(amibaIds)){
            return new ArrayList<>();
        }
        return amibaStaffMapper.selectByAmibaIds2(amibaIds);
    }

    @Override
    public List<AmibaStaff> findByAmibaCode(AmibaCode amibaCode) {
        if (Objects.isNull(amibaCode)) {
            return new ArrayList<>();
        }
        List<AmibaStaff> amibaStaffs = amibaStaffMapper.selectByAmibaCode(amibaCode);
        for (AmibaStaff amibaStaff : amibaStaffs) {
            amibaStaff.setRoleName(AmibaStaffRole.valueOf(amibaStaff.getRole()).getDesc());
        }
        return amibaStaffs;
    }

    @Override
    public List<AmibaStaff> findByStaffCode(String staffCode) {
        if (StringUtils.isEmpty(staffCode)) {
            return null;
        }
        return amibaStaffMapper.selectByStaffCode(staffCode);
    }

    @Override
    public AmibaStaff findSingleByStaffCode(String staffCode) {
        List<AmibaStaff> amibaStaffs = findByStaffCode(staffCode);
        return amibaStaffs.stream().findFirst().orElse(null);
    }

    @Override
    public AmibaStaff findByStaffCodeAndAmibaId(String staffCode, Integer amibaId) {
        return amibaStaffMapper.selectByStaffCodeAndAmibaId(staffCode, amibaId);
    }

    @Override
    public List<AmibaStaff> findByStaffCodeAndAmibaCode(String staffCode, AmibaCode amibaCode) {
        return amibaStaffMapper.selectByStaffCodeAndAmibaCode(staffCode, amibaCode);
    }

    @Override
    public List<AmibaStaff> findManagerByAmibaCode(AmibaCode code) {
        AssertUtils.notNull(code,"code为空！");
        return amibaStaffMapper.selectManagerByAmibaCode(code);
    }

    @Override
    public AmibaStaff findById(Integer id) {
        AssertUtils.notNull(id,"id为空！");
        return amibaStaffMapper.selectByPrimaryKey(id);
    }

    /**
     * 通过阿米巴编号过滤
     *
     * @param list      员工所属巴列表
     * @param amibaCode 阿米巴编号
     * @return 过滤后列表
     */
    @Override
    public List<AmibaStaff> filterByAmibaCode(List<AmibaStaff> list, AmibaCode amibaCode) {
        return list.stream().filter(item -> amibaCode.equals(item.getAmibaCode())).collect(Collectors.toList());
    }


    /**
     * 查询所有阿米巴成员
     */
    @Override
    public List<AmibaStaff> findAllAmibaStaff() {

        List<AmibaStaff> all = amibaStaffMapper.findAll();

        for (AmibaStaff amibaStaff : all) {
            Amiba amiba = amibaMapper.selectById(amibaStaff.getAmibaId());
            amibaStaff.setAmibaName(amiba.getName());
            amibaStaff.setOnlyUser( amibaStaff.getStaffCode() + "&" +amiba.getId());
        }
        return all;
    }

    @Override
    public List<AmibaStaff> findByCondition(AmibaStaff amibaStaff) {
        return amibaStaffMapper.selectByCondition(amibaStaff);
    }
}
